﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;

namespace Model
{
    [Serializable()]
    public class KhoanPhuCapReduceBOCollection : Csla.BusinessListBase<KhoanPhuCapReduceBOCollection, KhoanPhuCapReduceBO>
    {
        public Hashtable hstIndex = null; //ID,index

        #region BindingList Overrides
        protected override object AddNewCore()
        {
            KhoanPhuCapReduceBO _KhoanPhuCapReduceBO = KhoanPhuCapReduceBO.NewChildKhoanPhuCapReduceBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_KhoanPhuCapReduceBO));
            Add(_KhoanPhuCapReduceBO);
            return _KhoanPhuCapReduceBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private KhoanPhuCapReduceBOCollection()
        {
        }

        public static KhoanPhuCapReduceBOCollection NewKhoanPhuCapReduceBOCollection()
        {
            return new KhoanPhuCapReduceBOCollection();
        }

        public static KhoanPhuCapReduceBO NewKhoanPhuCapReduceBO()
        {
            return KhoanPhuCapReduceBO.NewChildKhoanPhuCapReduceBO();
        }

        public static KhoanPhuCapReduceBOCollection GetKhoanPhuCapReduceBOCollection()
        {
            SPParam spParam = new SPParam("KhoanPhuCapID", "ASC", "WHERE DELE=0");
            return DataPortal.Fetch<KhoanPhuCapReduceBOCollection>(spParam);
        }

        public static KhoanPhuCapReduceBOCollection GetKhoanPhuCapReduceBOCollection(bool getDeleted)
        {
            SPParam spParam = new SPParam("KhoanPhuCapID", "ASC", "");
            return DataPortal.Fetch<KhoanPhuCapReduceBOCollection>(spParam);
        }

        public static KhoanPhuCapReduceBOCollection GetKhoanPhuCapReduceBOCollection(string where)
        {
            SPParam spParam = new SPParam("KhoanPhuCapID", "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<KhoanPhuCapReduceBOCollection>(spParam);
        }

        public static KhoanPhuCapReduceBOCollection GetKhoanPhuCapReduceBOCollection(string where, bool getDeleted)
        {
            SPParam spParam = new SPParam("KhoanPhuCapID", "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<KhoanPhuCapReduceBOCollection>(spParam);
        }

        public static KhoanPhuCapReduceBOCollection GetKhoanPhuCapReduceBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "KhoanPhuCapID" : orderByColumn), "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<KhoanPhuCapReduceBOCollection>(spParam);
        }

        public static KhoanPhuCapReduceBOCollection GetKhoanPhuCapReduceBOCollection(string where, string orderByColumn, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "KhoanPhuCapID" : orderByColumn), "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<KhoanPhuCapReduceBOCollection>(spParam);
        }

        public static KhoanPhuCapReduceBOCollection GetKhoanPhuCapReduceBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "KhoanPhuCapID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<KhoanPhuCapReduceBOCollection>(spParam);
        }

        public static KhoanPhuCapReduceBOCollection GetKhoanPhuCapReduceBOCollection(string where, string orderByColumn, string orderByExpression, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "KhoanPhuCapID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<KhoanPhuCapReduceBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            hstIndex = new Hashtable();
            int NhanVienID = -1;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KhoanPhuCapReduceBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    KhoanPhuCapReduceBO objKhoanPhuCapReduceBO = KhoanPhuCapReduceBO.GetChildKhoanPhuCapReduceBO(dr);
                    if (NhanVienID != objKhoanPhuCapReduceBO.NhanVienID.Value)
                    {
                        NhanVienID = objKhoanPhuCapReduceBO.NhanVienID.Value;
                        hstIndex.Add(NhanVienID, this.Count);
                    }

                    this.Add(objKhoanPhuCapReduceBO);
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public KhoanPhuCapReduceBO GetKhoanPhuCapReduceBO(int _KhoanPhuCapID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].KhoanPhuCapID == _KhoanPhuCapID)
                    return this[i];

            return null;
        }

        public KhoanPhuCapReduceBOCollection FilterData(int NhanVienID, int Thang, int Nam)
        {
            KhoanPhuCapReduceBOCollection phuCapBOCollection = KhoanPhuCapReduceBOCollection.NewKhoanPhuCapReduceBOCollection();

            int index = int.MaxValue;
            if (hstIndex.ContainsKey(NhanVienID))
                index = (int)hstIndex[NhanVienID];

            int iCompare = Nam * 10000 + Thang * 100 + (int)Session.thietDatBOCollection.GetDoubleValue(LoaiThietDat.NgayTinhLuong);

            int length = this.Count;

            KhoanPhuCapReduceBO BO = null;
            for (int i = index; i < length; i++)
            {
                BO = this[i];

                if (BO.NhanVienID.Value != NhanVienID)
                    break;
                else
                    if (StaticUtil.GetIntFromDT(BO.NgayApDung) <= iCompare)
                        phuCapBOCollection.Add(this[i]);
            }

            return phuCapBOCollection;
        }

        public void Remove(int KhoanPhuCapID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
            {
                if (this[i].KhoanPhuCapID == KhoanPhuCapID)
                {
                    try
                    {
                        this.RemoveAt(i);
                    }
                    catch { }
                    break;
                }
            }
        }
        #endregion Business Method
        //==========================================================================
    }
}
